package org.microsleep;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

public class Util {

	public static <OUT, IN> Collection<OUT> map(Collection<IN> list,
			F<IN, OUT> f) {
		Collection<OUT> result = new ArrayList<OUT>(list.size());
		for (IN item : list) {
			result.add(f.f(item));
		}
		return result;
	}

	public static String concat(Collection<String> list, String separator) {
		StringBuilder builder = new StringBuilder();
		Iterator<String> i = list.iterator();
		if (i.hasNext()) {
			builder.append(i.next());
		}
		while (i.hasNext()) {
			builder.append(separator);
			builder.append(i.next());
		}
		return builder.toString();
	}

	public static void notNull(Object arg) {
		if (arg == null) {
			throw new IllegalArgumentException("Parameter is null");
		}
	}

	public static void notNull(Object arg, String message) {
		if (arg == null) {
			throw new IllegalArgumentException(message);
		}
	}

	public static void isTrue(boolean condition, String message) {
		if (!condition) {
			if (message == null) {
				message = "Condition was not met";
			}
			throw new IllegalArgumentException(message);
		}
	}

	public interface F<IN, OUT> {
		public OUT f(IN in);
	}
}
